﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class UnitUpgradeEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var elHost = new ElementHost
                {
                    Width = 360,
                    Height = 420,
                    Child = new UCUnitUpgrade((List<comm.objst.UnitUpgrade>)value)
                };
                edSvc.DropDownControl(elHost);
                return value;
            }
            return value;
        }
    }

    /// <summary>
    /// UCUnitUpgrade.xaml 的交互逻辑
    /// </summary>
    public partial class UCUnitUpgrade : UserControl
    {
        List<comm.objst.UnitUpgrade> _upgradeList = null;

        public UCUnitUpgrade(List<comm.objst.UnitUpgrade> upgradeList)
        {
            InitializeComponent();

            _upgradeList = upgradeList;

            Fill();

            btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
            btnClear.Click += new RoutedEventHandler(btnClear_Click);
        }

        void btnClear_Click(object sender, RoutedEventArgs e)
        {
            _upgradeList.Clear();
            Fill();
        }
        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            _upgradeList.Add(new objst.UnitUpgrade { NeedTechLevel = 1, UnitId = "" });
            Fill();
        }

        void Fill()
        {
            spList.Children.Clear();

            foreach (var item in _upgradeList)
            {
                var item2 = item;
                var grid = new Grid { Margin = new Thickness(0, 0, 0, 4) };

                #region tbCount
                var tbLevel = new TextBox
                {
                    Width = 120,
                    Text = item.NeedTechLevel.ToString(),
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Center
                };
                tbLevel.TextChanged += new TextChangedEventHandler((o, e) =>
                {
                    uint n = 0;
                    if (uint.TryParse(tbLevel.Text, out n))
                    {
                        item2.NeedTechLevel = n;
                        tbLevel.Opacity = 1;
                    }
                    else
                    {
                        tbLevel.Opacity = .5;
                    }
                });
                grid.Children.Add(tbLevel);
                #endregion

                #region cb
                var cb = new ComboBox
                {
                    Margin = new Thickness(124, 0, 24, 0),
                    VerticalAlignment = VerticalAlignment.Center
                };
                var units = DefineFile.LastInstance.GetAllUnit();
                foreach (var unitId in units)
                {
                    var unit = DefineFile.LastInstance.GetUnit(unitId);
                    var tb = new TextBlock { Tag = unitId, Text = unitId + "(" + unit.UnitType + ")" };
                    cb.Items.Add(tb);
                    if (unitId == item.UnitId)
                    {
                        cb.SelectedItem = tb;
                    }
                }
                cb.SelectionChanged += new SelectionChangedEventHandler((o, e) =>
                {
                    var tb = (TextBlock)cb.SelectedItem;
                    item2.UnitId = tb.Tag.ToString();
                });
                grid.Children.Add(cb);
                #endregion

                #region btnDel
                var btnDel = new Button
                {
                    Content = "X",
                    Width = 22,
                    Height = 22,
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Right
                };
                btnDel.Click += new RoutedEventHandler((o, e) =>
                {
                    _upgradeList.Remove(item2);
                    Fill();
                });
                grid.Children.Add(btnDel);
                #endregion

                spList.Children.Add(grid);
            }
        }
    }
}
